package com.wingscode.modules.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wingscode.common.utils.PageUtils;
import com.wingscode.common.utils.Query;
import com.wingscode.modules.common.dao.ChatObjectDao;
import com.wingscode.modules.common.entity.ChatDetailEntity;
import com.wingscode.modules.common.entity.ChatObjectEntity;
import com.wingscode.modules.common.service.ChatDetailService;
import com.wingscode.modules.common.service.ChatObjectService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;


@Service("chatObjectService")
public class ChatObjectServiceImpl extends ServiceImpl<ChatObjectDao, ChatObjectEntity> implements ChatObjectService {
    @Resource
    private ChatDetailService chatDetailService;

    @Override
    public PageUtils chatList(Map<String, Object> params) {
        Integer agentId = null, userId = null, state = null;

        if (params.containsKey("agentId"))
            agentId = Integer.parseInt(params.get("agentId").toString());
        if (params.containsKey("userId"))
            userId = Integer.parseInt(params.get("userId").toString());
        if (params.containsKey("state"))
            state = Integer.parseInt(params.get("state").toString());

        IPage<ChatObjectEntity> page = this.baseMapper.getObject(new Query<ChatObjectEntity>().getPage(params), agentId, userId, state);
        if (page.getRecords() != null && page.getRecords().size() > 0) {
            for (ChatObjectEntity object : page.getRecords()) {
                // 获得未读消息数
                Integer unreadNum = 0;
                if (agentId == object.getFromAgentId() && userId == object.getFromUserId()) {
                    unreadNum = chatDetailService.count(
                            new QueryWrapper<ChatDetailEntity>()
                                    .eq("chat_id", object.getId())
                                    .eq("is_read", 0)
                                    .isNull("from_agent_id")
                                    .isNull("from_user_id")
                    );
                } else if (agentId == object.getToAgentId() && userId == object.getToUserId()) {
                    unreadNum = chatDetailService.count(
                            new QueryWrapper<ChatDetailEntity>()
                                    .eq("chat_id", object.getId())
                                    .eq("is_read", 0)
                                    .isNull("to_agent_id")
                                    .isNull("to_user_id")
                    );
                }
                object.setUnreadNum(unreadNum);
            }
        }

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryObject(Map<String, Object> params) {
        Integer type = null;
        String agentName = "";

        if (params.containsKey("type"))
            type = Integer.parseInt(params.get("type").toString());
        if (params.containsKey("agentName"))
            agentName = (String) params.get("agentName");

        IPage<ChatObjectEntity> page = this.baseMapper.queryObject(new Query<ChatObjectEntity>().getPage(params), type, agentName);

        return new PageUtils(page);
    }

}
